﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using StedySoft.SenseSDK;
using Microsoft.Drawing;
using Interfaces;
using Map;
using StedySoft.SenseSDK.DrawingCE;

namespace Transportr
{
    public partial class MainForm : Form
    {
        public enum ViewState { Map, Info, Split };

        UserControl InfoControl;
        ViewState currentViewState = ViewState.Split;
        int infoControlHeight;

        public MainForm()
        {
            InitializeComponent();
            // TODO: show some splash screen here!
            infoControlHeight = this.Height / 2;
            Global.Init();
            Global.map.Hold += new GeoEventHandler(map_Hold);

            this.SuspendLayout();
            this.Controls.Add(Global.map);
            this.changePanel(new MenuItem() { Text = "Favorites" }, EventArgs.Empty);
            this.ChangeView(ViewState.Split);
            
            this.ResumeLayout();
        }

        void map_Hold(object sender, GeoEventArgs e)
        {
            GeoPoint current = Global.map.QueryMarker(Global.UserPositionMarkerId);
            Global.map.MoveMarker(Global.UserPositionMarkerId, e.EventPoint.Latitude, e.EventPoint.Longitude);
            if (SenseAPIs.SenseMessageBox.Show("Are you sure you want to reposition yourself to the specified point?",
                                               "Reposition",
                                               SenseMessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Global.m_middleware.LockPosition(new GPSData()
                {
                    Latitude = e.EventPoint.Latitude,
                    Longitude = e.EventPoint.Longitude,
                    Time = DateTime.Now
                });
            }
            else
            {
                Global.map.MoveMarker(Global.UserPositionMarkerId, current.Latitude, current.Longitude);
            }
        }

        public void ChangeView(ViewState state)
        {
            if (InfoControl == null)
            {
                state = ViewState.Map;
            }
            this.SuspendLayout();
            switch (state)
            {
                case ViewState.Info:
                    InfoControl.Visible = true;
                    InfoControl.Dock = DockStyle.Fill;
                    InfoControl.Height = this.Height;
                    InfoControl.BringToFront();
                    Global.map.Visible = false;
                    break;
                case ViewState.Map:
                    InfoControl.Visible = false;
                    Global.map.Visible = true;
                    Global.map.Dock = DockStyle.Fill;
                    Global.map.BringToFront();
                    break;
                case ViewState.Split:
                    Global.map.Visible = true;
                    Global.map.Height = this.Height - infoControlHeight;
                    Global.map.Dock = DockStyle.Bottom;
                    InfoControl.Visible = true;
                    InfoControl.Dock = DockStyle.Top;
                    InfoControl.Height = infoControlHeight;
                    InfoControl.BringToFront();
                    break;
            }
            this.ResumeLayout();
            currentViewState = state;
        }

        private void mitmSettings_Click(object sender, EventArgs e)
        {
            SettingsForm frm = new SettingsForm();
            frm.ShowDialog();
        }

        private void mitmToggleMap_Click(object sender, EventArgs e)
        {
            switch (this.currentViewState)
            {
                case ViewState.Map:
                    this.ChangeView(ViewState.Info);
                    break;
                case ViewState.Info:
                    this.ChangeView(ViewState.Split);
                    break;
                case ViewState.Split:
                    this.ChangeView(ViewState.Map);
                    break;
            }
        }

        private void changePanel(object sender, EventArgs e)
        {
            string senderStr = (sender as MenuItem).Text;
            
            this.SuspendLayout();
            if (InfoControl != null)
            {
                this.Controls.Remove(InfoControl);
                InfoControl.Dispose();
            }

            switch (senderStr)
            {
                case "Favorites":
                    InfoControl = new CtlFavorites() { Dock = DockStyle.Top, Height = this.Height / 2 };
                    mitmNearStops.Checked = false;
                    mitmBrowse.Checked = false;
                    mitmFavorites.Checked = true;
                    break;
                case "Look around":
                    InfoControl = new CtlStationList() { Dock = DockStyle.Top, Height = this.Height / 2 };
                    mitmNearStops.Checked = true;
                    mitmFavorites.Checked = false;
                    mitmBrowse.Checked = false;
                    break;
                case "Browse":
                    InfoControl = new CtlBrowse() { Dock = DockStyle.Top, Height = this.Height / 2 };
                    mitmNearStops.Checked = false;
                    mitmFavorites.Checked = false;
                    mitmBrowse.Checked = true;
                    break;
                default:
                    MessageBox.Show("No panel for this item!");
                    break;
            }

            this.Controls.Add(InfoControl);
            this.ChangeView(ViewState.Split);
            this.ResumeLayout();
        }

        private void mitmClearCache_Click(object sender, EventArgs e)
        {
            if (Service.Cache.Valid)
            {
                Service.Cache.Clear();
            }
            MessageBox.Show("Service cache cleared.");
        }
    }
}